using Contoso_University.Models;

namespace Contoso_University.Data
{
    public static class DbInitializer
    {
        public static void Initialize(SchoolContext context)
        {
  
            context.Database.EnsureCreated();

  
            if (context.Students.Any())
            {
                return;   
            }

            //添加部门
            var departments = new Department[]
            {
                new Department { Name = "英语系", Budget = 350000, StartDate = DateTime.Parse("2007-09-01") },
                new Department { Name = "数学系", Budget = 100000, StartDate = DateTime.Parse("2007-09-01") },
                new Department { Name = "工程系", Budget = 350000, StartDate = DateTime.Parse("2007-09-01") },
                new Department { Name = "经济系", Budget = 100000, StartDate = DateTime.Parse("2007-09-01") }
            };

            context.Departments.AddRange(departments);
            context.SaveChanges();

            //添加讲师
            var instructors = new Instructor[]
            {
                new Instructor { FirstName = "伞", LastName = "三", HireDate = DateTime.Parse("1995-03-11") },
                new Instructor { FirstName = "成", LastName = "蒙", HireDate = DateTime.Parse("2002-07-06") },
                new Instructor { FirstName = "户", LastName = "号", HireDate = DateTime.Parse("1998-07-01") },
                new Instructor { FirstName = "七", LastName = "未", HireDate = DateTime.Parse("2001-01-15") },
                new Instructor { FirstName = "姚", LastName = "舜", HireDate = DateTime.Parse("2004-02-12") }
            };

            context.Instructors.AddRange(instructors);
            context.SaveChanges();

   
            departments[0].InstructorID = instructors[0].ID;
            departments[1].InstructorID = instructors[1].ID;
            departments[2].InstructorID = instructors[2].ID;
            departments[3].InstructorID = instructors[3].ID;

            context.SaveChanges();

            //添加课程
            var courses = new Course[]
            {
                new Course { CourseID = 1050, Title = "化学", Credits = 3, DepartmentID = departments[2].DepartmentID },
                new Course { CourseID = 4022, Title = "微观经济学", Credits = 3, DepartmentID = departments[3].DepartmentID },
                new Course { CourseID = 4041, Title = "宏观经济学", Credits = 3, DepartmentID = departments[3].DepartmentID },
                new Course { CourseID = 1045, Title = "微积分", Credits = 4, DepartmentID = departments[1].DepartmentID },
                new Course { CourseID = 3141, Title = "三角学", Credits = 4, DepartmentID = departments[1].DepartmentID },
                new Course { CourseID = 2021, Title = "作文", Credits = 3, DepartmentID = departments[0].DepartmentID },
                new Course { CourseID = 2042, Title = "文学", Credits = 4, DepartmentID = departments[0].DepartmentID }
            };

            context.Courses.AddRange(courses);
            context.SaveChanges();

            //添加课程分配
            var courseAssignments = new CourseAssignment[]
            {
                new CourseAssignment { CourseID = courses[0].CourseID, InstructorID = instructors[0].ID },
                new CourseAssignment { CourseID = courses[1].CourseID, InstructorID = instructors[1].ID },
                new CourseAssignment { CourseID = courses[2].CourseID, InstructorID = instructors[1].ID },
                new CourseAssignment { CourseID = courses[3].CourseID, InstructorID = instructors[2].ID },
                new CourseAssignment { CourseID = courses[4].CourseID, InstructorID = instructors[2].ID },
                new CourseAssignment { CourseID = courses[5].CourseID, InstructorID = instructors[3].ID },
                new CourseAssignment { CourseID = courses[6].CourseID, InstructorID = instructors[3].ID }
            };

            context.CourseAssignments.AddRange(courseAssignments);
            context.SaveChanges();

            //添加学生
            var students = new Student[]
            {
                new Student { FirstName = "张", LastName = "是", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "五", LastName = "人", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "如", LastName = "放", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "海", LastName = "和", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "万", LastName = "里", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "才", LastName = "热", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "深", LastName = "关", EnrollmentDate = DateTime.Parse("2025-09-01") },
                new Student { FirstName = "升", LastName = "高", EnrollmentDate = DateTime.Parse("2025-09-01") }
            };

            context.Students.AddRange(students);
            context.SaveChanges();

            //添加注册
            var enrollments = new Enrollment[]
            {
                new Enrollment { StudentID = students[0].ID, CourseID = courses[0].CourseID, Grade = Grade.A },
                new Enrollment { StudentID = students[0].ID, CourseID = courses[1].CourseID, Grade = Grade.C },
                new Enrollment { StudentID = students[0].ID, CourseID = courses[2].CourseID, Grade = Grade.B },
                new Enrollment { StudentID = students[1].ID, CourseID = courses[3].CourseID, Grade = Grade.B },
                new Enrollment { StudentID = students[1].ID, CourseID = courses[4].CourseID, Grade = Grade.F },
                new Enrollment { StudentID = students[1].ID, CourseID = courses[5].CourseID, Grade = Grade.F },
                new Enrollment { StudentID = students[2].ID, CourseID = courses[0].CourseID },
                new Enrollment { StudentID = students[3].ID, CourseID = courses[0].CourseID },
                new Enrollment { StudentID = students[3].ID, CourseID = courses[1].CourseID, Grade = Grade.F },
                new Enrollment { StudentID = students[4].ID, CourseID = courses[2].CourseID, Grade = Grade.C },
                new Enrollment { StudentID = students[5].ID, CourseID = courses[3].CourseID },
                new Enrollment { StudentID = students[6].ID, CourseID = courses[4].CourseID, Grade = Grade.A }
            };

            context.Enrollments.AddRange(enrollments);
            context.SaveChanges();
        }
    }
} 